<?php
/**
 * 用户类
 * @author chenlisong E-mail:chenlisong1021@163.com 
 * @version 创建时间：2016年10月24日 上午11:58:51 
 * 类说明
 */
class UserAction extends BaseAction{
	
	public function index(){
		echo $this->err("非法操作！");
	}
	
	/**
	 * 修改登录密码
	 * Enter description here ...
	 */
	public function modifyPwd(){
		$User = M('User');$LoginSession = M('LoginSession');
		$param = $this->param;
		if(empty($param['newPassword']) || empty($param['oldPassword'])){
			echo $this->err("参数不能为空！");
			return ;
		}
		$info = $User->where("id = ".$param['userId'])->find();
		if($info['password'] == $param['oldPassword']){//原密码输入错误
			if($info['password'] == $param['newPassword']){
				echo $this->err("修改的密码不能和原密码一样！");
				return ;
			}
			$data['password'] = $param['newPassword'];
			$result= $User->where("id = ".$param['userId'])->save($data);
			if($result !== false){
				$where['session_id'] = $param['sessionId'];
				$where['user_id'] = $param['userId'];
				$LoginSession->where($where)->delete();
				//清空极光推送ID
				$User->where('id = '.$param['userId'])->setField('jpush_id','');
				echo $this->suc();
			}else{
				echo $this->err("修改失败！");
			}
		}else{
			echo $this->err("原密码错误！");
		}
	}
	
	/**
	 * 收货地址列表
	 */
	public function addressList(){
		$Address = M('Address');$School = M('School');
		$param = $this->param;
		$list = $Address->where('flag = 1 and user_id='.$param['userId'])->field('id,province,city,area,address,floor_num,
				contact_name as contactName,contact_tel as contactTel,is_main as isMain,school_id as schoolId')->select();
		foreach($list as $key=>$val){
			$list[$key]['schoolName'] = (string)$School->where('id = '.$val['schoolId'])->getField('name');
		}
		echo $this->suc($list,0);
	}
	
	/**
	 * 新增收货地址
	 */
	public function addAddress(){
		$Address = M('Address');
		$param = $this->param;
		if(empty($param['contactName']) || !checkPhone($param['contactTel'])){
			echo $this->err("错误的联系人或联系电话！");
			return ;
		}
		if(empty($param['province']) || empty($param['city']) || empty($param['area']) || empty($param['address'])){
			echo $this->err("地址信息不能为空！");
			return ;
		}
		//检查是否超出上限
		$where['user_id'] = $param['userId'];
		$where['flag'] = 1;
		$num = $Address->where($where)->count();
		if($num >= 5){
			echo $this->err("新增失败，每个用户最多只能添加5个地址信息！");
			return ;
		}
		$data['school_id'] = $param['schoolId'];
		$data['province'] = $param['province'];
		$data['city'] = $param['city'];
		$data['area'] = $param['area'];
		$data['address'] = $param['address'];
		$data['floor_num'] = $param['floor_num'];
		$data['contact_name'] = $param['contactName'];
		$data['contact_tel'] = $param['contactTel'];
		$data['user_id'] = $param['userId'];
		$data['is_main'] = 1;
		$data['create_time'] = time();
		$Address->where($where)->setField('is_main',0);
		$result = $Address->data($data)->add();
		if($result){
			echo $this->suc();
		}else{
			echo $this->err("新增失败！");
		}
	}
	
	/**
	 * 修改收货地址
	 */
	public function editAddress(){
		$Address = M('Address');
		$param = $this->param;
		if(empty($param['contactName']) || !checkPhone($param['contactTel'])){
			echo $this->err("错误的联系人或联系电话！");
			return ;
		}
		if(empty($param['province']) || empty($param['city']) || empty($param['area']) || empty($param['address'])){
			echo $this->err("地址信息不能为空！");
			return ;
		}
		$where['id'] = $param['id'];
		$where['user_id'] = $param['userId'];
		$where['flag'] = 1;
		$count = $Address->where($where)->count();
		if($count == 0){
			echo $this->err("地址不存在或未找到！");
			return ;
		}
		$data['school_id'] = $param['schoolId'];
		$data['province'] = $param['province'];
		$data['city'] = $param['city'];
		$data['area'] = $param['area'];
		$data['address'] = $param['address'];
		$data['floor_num'] = $param['floor_num'];
		$data['contact_name'] = $param['contactName'];
		$data['contact_tel'] = $param['contactTel'];
		$data['update_time'] = time();
		if (false !== $Address->where($where)->save($data)) {
			echo $this->suc();
		} else {
			echo $this->err("修改失败！");
		}
	}
	
	/**
	 * 删除地址
	 */
	public function deleteAddress(){
		$Address = M('Address');
		$param = $this->param;
		$where['id'] = $param['id'];
		$where['user_id'] = $param['userId'];
		$where['flag'] = 1;
		$count = $Address->where($where)->count();
		if($count == 0){
			echo $this->err("地址不存在或未找到！");
			return ;
		}
		$ismain = $Address->where('id = '.$param['id'])->getField('is_main');
		if($ismain == 1){//如果删除的是主应用，删除主应用后设置下一条数据为主应用
			$Address->where('user_id = '.$param['userId'].' and flag = 1 and id <>'.$param['id'])->order('id desc')->limit(1)->setField('is_main',1);
		}
		$result = $Address->where('flag = 1 and id = '.$param['id'])->setField('flag',2);
		if($result){
			echo $this->suc();
		}else{
			echo $this->err("修改失败！");
		}
	}
	
	/**
	 * 设置默认收货地址
	 * Enter description here ...
	 */
	public function setDefaultAddress(){
		$Address = M('Address');
		$param = $this->param;
		// 只有一个主应用
		$Address->where('user_id = '.$param['userId'])->setField("is_main",0);
		$result = $Address->where('flag = 1 and id = '.$param['id'])->setField('is_main',1);
		if($result){
			echo $this->suc();
		}else{
			echo $this->err("修改失败！");
		}
	}
	
	/**
	 * 获取余额
	 */
	public function getBalance(){
		$User = M('User');$Order = M('Order');$Shop = M("Shop");
		$param = $this->param;
		$where['flag'] = 1;
		$where['id'] = $param['userId'];
		$userInfo = $User->where($where)->field('balance,is_shop')->find();
		$data['balance'] = $userInfo['balance'];
		//获取推广人数
		$data['extendNum'] = (int) $User->where('flag = 1 and parent_id = '.$param['userId'])->count();

		//我的订单
		$data['not_pay_order_num'] = (int) $Order->where('flag = 1 and user_id = '.$param['userId'].' and status = 1 ')->count(); //待付款
		$data['pending_receipt_order_num'] = (int) $Order->where('flag = 1 and user_id = '.$param['userId'].' and (status = 2 or status = 3)')->count(); //待收货
		$data['Refund_order_num'] = (int) $Order->where('flag = 1 and user_id = '.$param['userId'].' and (status = 0 or status = -3 or status = -2)')->count();  //退款
		$data['my_shop_pending_order_num'] = 0;
		$data['my_shop_sending_order_num'] = 0;
		$data['my_shop_finish_order_num'] = 0;
		if($userInfo['is_shop']==1){
			$Shop_data = $Shop->where(array("user_id"=>$param['userId']))->find();
			$shop_id = $Shop_data['id'];
			$data['my_shop_status'] = $Shop_data['status'];
			$data['my_shop_pending_order_num'] = (int) $Order->where('flag = 1 and shop_id = '.$shop_id.' and status = 2')->count(); //待处理
			$data['my_shop_sending_order_num'] = (int) $Order->where('flag = 1 and shop_id = '.$shop_id.' and status = 3')->count(); //派送中
			$data['my_shop_finish_order_num'] = (int) $Order->where('flag = 1 and shop_id = '.$shop_id.' and status = 4')->count();  //完成
			$data['my_shop_refund_order_num'] = (int) $Order->where('flag = 1 and shop_id = '.$shop_id.' and  status = 0 ')->count();  //退货
		}
		echo $this->suc($data);
	}
	
	/**
	 * 获取账单
	 */
	public function userDetailsList(){
		$UserDetails = M('UserDetails');
		$param = $this->param;
		if(is_null($param['nextStartId'])){
			echo $this->err('参数错误！');
			return ;
		}
		$field='id,title,type,create_time as createTime,price,last_price as lastPrice';
		$where['user_id'] = $param['userId'];
		$where['flag'] = 1;
		$where['create_time'] = array('gt',time()-(30*86400));//读取最近一个月的记录
		$count = $UserDetails->where($where)->count();
		if($count < 10){
			$nextStartId = -1;
			$list = $UserDetails->where($where)->field($field)->order('create_time desc')->select();
		}else{
			$nextStartId = $param['nextStartId'];
			$list = $UserDetails->where($where)->field($field)->limit($nextStartId,10)->order('create_time desc')->select();
			$nextStartId = $nextStartId + 10;
			if($nextStartId >= $count || count($list) == 0){
				$nextStartId = -1;
			}
		}
		echo $this->suc($list,$nextStartId);
	}
	
	/**
	 * 提交意见
	 */
	public function addSuggestion(){
		$Suggestion = M('Suggestion');
		$param = $this->param;
		if(empty($param['content'])){
			echo $this->err('至少说点什么吧？');
			return ;
		}
		$data['user_id'] = $param['userId'];
		$data['content'] = $param['content'];
		$data['create_time'] = time();
		$result = $Suggestion->add($data);
		if($result){
			echo $this->suc();
		}else{
			echo $this->err("新增失败！");
		}
	}
	
	/**
	 * 推广列表
	 */
	public function extendList(){
		$User = M('User');$UserDetails = M('UserDetails');
		$param = $this->param;
		//获取推广人数
		$data['num'] = $User->where('flag = 1 and parent_id = '.$param['userId'])->count();
		$where = 'type = 1 and from_user_id > 0 and flag = 1 and user_id = '.$param['userId'];
		$data['price'] = (float)$UserDetails->where($where)->sum('price');
		$data['detailsList'] = array();
		//获取明细
		$list = $User->where('flag = 1 and parent_id = '.$param['userId'])->field('id,tel')->order('create_time desc')->select();
		foreach ($list as $key=>$val){
			$data['detailsList'][$key]['username'] = $val['tel'];
			$data['detailsList'][$key]['price'] = (float) $UserDetails->where('flag = 1 and type = 1 and from_user_id = '.$val['id'].' and user_id = '.$param['userId'])->getField('SUM(price) as price');
		}
		echo $this->suc($data);
	}
	
	/**
	 * 去充值
	 */
	public function goRecharge(){
		 $RechargeSetting = M('RechargeSetting');
		 $param = $this->param;
		 $data = $RechargeSetting->where('flag = 1')->field('id,recharge_price as rechargePrice,give_price as givePrice')->select();
		 echo $this->suc($data, 0);
	}
	
	/**
	 * 收藏商品/取消收藏
	 */
	public function productCollection(){
		$ProductCollection = M('ProductCollection');
		$param = $this->param;
		$map['user_id'] = $param['userId'];
		$map['product_id'] = $param['productId'];
	    $count = $ProductCollection->where($map)->count();
		if($count == 0){//收藏
			$result = $ProductCollection->add($map);
		}else{
			$result = $ProductCollection->where($map)->delete();
		}
		if($result){
			echo $this->suc();
		}else{
			echo $this->err('操作失败！');
		}
	}
	
	/**
	 * 修改绑定手机号
	 * Enter description here ...
	 */
	public function modifyTel(){
		$User = M('User');$LoginSession = M('LoginSession');
		$param = $this->param;
		if(!checkPhone($param['oldTel']) || !checkPhone($param['newTel'])){
			echo $this->err("错误的手机号！");
			return ;
		}
		if($param['newTel'] == $param['oldTel']){
			echo $this->err('新手机号不能与旧手机号相同！');
			return ;
		}
		if(empty($param['phoneCode'])){
			echo $this->err('验证码为空！');
			return ;
		}
		if($param['newTel'] != session('send_code_tel')){
			echo $this->err("请重新获取验证码！");
			return ;
		}
		if($param['phoneCode'] != session('tel_code')){
			echo $this->err("验证码错误！");
			return ;
		}
		$where['flag'] = 1;
		$where['tel'] = $param['newTel'];
		//判断新手机号是否已经被注册
		$count = $User->where($where)->count();
		if($count != 0){
			echo $this->err("该手机号已被注册！");
			return ;
		}
		$info = $User->where("id = ".$param['userId'])->find();
		if($info['tel'] != $param['oldTel']){//验证原手机号是否正确
			echo $this->err('原手机号填写错误！');
			return ;
		}
		$data['tel'] = $param['newTel'];
		$data['username'] = $param['newTel'];
		$data['update_time'] = time();
		$result = $User->where("id = ".$param['userId'])->save($data);
		if($result){
			//删除手机端session
			$LoginSession->where('user_id = '.$param['userId'])->delete();
			echo $this->suc();
		}else{
			echo $this->err("修改失败！");
		}
	}
	
	/**
	 * 消息列表
	 */
	public function getNoticeList(){
		$Notice = M('Notice');
		$param = $this->param;
		if(is_null($param['nextStartId'])){
			echo $this->err('参数错误！');
			return ;
		}
		$field='id,type,message,ambulance,create_time as createTime';
		$where = '(is_all = 1 and user_id = '.$param['userId'].') OR (is_all = 2) and flag = 1';
		$count = $Notice->where($where)->count();
		if($count < 10){
			$nextStartId = -1;
			$list = $Notice->where($where)->field($field)->order('create_time desc')->select();
		}else{
			$nextStartId = $param['nextStartId'];
			$list = $Notice->where($where)->field($field)->limit($nextStartId,10)->order('create_time desc')->select();
			$nextStartId = $nextStartId + 10;
			if($nextStartId >= $count || count($list) == 0){
				$nextStartId = -1;
			}
		}
		echo $this->suc($list,$nextStartId);
	}
	
}